<template>
  <div class="Login">
    <div class="LoginContainer">
      <div class="LoginBox">
        <div class="LoginHeader">
          <img src="@/assets/logo.png" class="Logo" alt=""/>
          <h2>IT培优综合服务平台</h2>
        </div>
        <el-form ref="form" :model="form" :rules="rules" label-width="0px">
          <el-form-item prop="email">
            <el-input
                v-model="form.email"
                placeholder="请输入邮箱"
                prefix-icon="el-icon-message"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                v-model="form.password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="el-icon-lock"
            ></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input
                v-model="form.confirmPassword"
                type="password"
                placeholder="请确认密码"
                prefix-icon="el-icon-check"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
            class="SubmitButton"
            type="primary"
            @click="updatePassword"
        >
          确认修改
        </el-button>
        <div class="Footer">
          <img src="@/assets/忘记密码.png" class="FooterIcon" alt=""/>
          <el-link @click="backLogin" class="FooterLink">返回登录</el-link>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import CryptoJS from "crypto-js";

export default {
  data() {
    return {
      form: {
        email: "",
        password: "",
        confirmPassword: "",
      },
      rules: {
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: this.checkPassword, trigger: 'blur' }
        ],
      },
      timer: null // 新增用于存储定时器标识的变量
    };
  },
  methods: {
    // 自定义验证确认密码
    checkPassword(rule, value, callback) {
      if (value !== this.form.password) {
        callback(new Error('两次密码输入不一致'));
      } else {
        callback();
      }
    },
    updatePassword() {
      // 清除之前的定时器，若存在的话
      if (this.timer) {
        clearTimeout(this.timer);
      }
      this.timer = setTimeout(() => {
        this.$refs.form.validate((valid) => {
          if (valid) {
            // 使用MD5对密码进行加密
            const encryptedPassword = CryptoJS.MD5(this.form.password).toString();
            const encryptedForm = {...this.form, password: encryptedPassword, confirmPassword: encryptedPassword };

            this.request.post("/web/updatePassword", encryptedForm).then(res => {
              if (res.code === "200") {
                this.messageSuccess("密码修改成功");
                setTimeout(() => {
                  this.$router.push({ name: "login" });
                }, 1000);
              } else {
                this.messageError(res.msg);
              }
            });
          }else{
            this.messageError("请先完善表单信息");
          }
        });
        this.timer = null; // 执行完后重置定时器标识
      }, 1000);
    },
    backLogin() {
      this.$router.push({ name: "login" });
    },
  },
};
</script>
<style lang="scss" scoped>
.Login {
  background: url("@/assets/登录背景.jpeg") no-repeat center center fixed;
  height: 100vh;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;

  .LoginContainer {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;

    .LoginBox {
      background-color: white; /* 修改框背景为白色 */
      padding: 30px;
      border-radius: 10px;
      min-width: 350px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* 添加阴影，使框体更加突出 */

      .LoginHeader {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 20px;
      }

      .Logo {
        height: 30px;
        width: 30px;
      }

      h2 {
        color: black;
        margin-left: 10px;
      }

      .SubmitButton {
        width: 85%;
        margin-top: 20px;
      }

      .Footer {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 20px;
      }

      .FooterIcon {
        height: 20px;
        width: 20px;
      }

      .FooterLink {
        margin-left: 10px;
        color: black;
      }
    }
  }
}
</style>

